package network;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-10-17
 * Time: 17:00
 */
//UDP版本的回显客户端
public class UdpEchoClient {
    private DatagramSocket socket = null;
    private String serverIp = null;
    private int serverPort = 0;


    //一次通信需要有两个ip，两个端口
    //客户端的ip是127.0.0.1已知的
    //客户端端口port是系统自动分配的
    //服务器的ip和端口也需要告诉客户端，才能顺利的把消息发给服务器
    public UdpEchoClient(String serverIp,int serverPort) throws SocketException {
        socket = new DatagramSocket();
        this.serverIp = serverIp;
        this.serverPort = serverPort;

    }
    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner scanner = new Scanner(System.in);
        while (true){
            //1.从控制台读取要发送的数据
            System.out.println("> ");
            String request = scanner.next();
            if(request.equals("exit")){
                System.out.println("goodbye");
                break;
            }
            //2.构造UDP请求，并发送
            //构造这个Packet的时候，需要把serverIp和port都传入过来，此处的Ip地址需要填写的是32位的整数形式
            //上述的serveIp是一个字符串，需要使用InetAddress.getByName 来进行转换
            DatagramPacket requestPacket  = new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(serverIp),serverPort);
            socket.send(requestPacket);
            //3.读取服务器的UDP响应，并解析
            DatagramPacket responsePacket = new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String response = new String(responsePacket.getData(),0,responsePacket.getLength());

            //4.把解析好的结果显示出来
            System.out.println(response);

        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient client = new UdpEchoClient("127.0.0.1",4000);
        client.start();
    }
}
